package com.tang4j.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tang4j.core.controller.AbstractController;
import com.tang4j.core.model.ResponseModel;
import com.tang4j.core.model.easyui.DataGrid;
import com.tang4j.core.model.factory.ResponseModelFactory;
import com.tang4j.model.ThSysDict;
import com.tang4j.service.ThSysDictService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sysDict")
public class ThSysDictController extends AbstractController<ThSysDictService, ThSysDict> {

    public ThSysDictController(ThSysDictService thSysDictService) {
        super(thSysDictService);
    }

    @PostMapping("/getType")
    public ResponseModel getType() {
        try {
            QueryWrapper<ThSysDict> wrapper = new QueryWrapper<>();
            wrapper.isNull("pid");
            List<ThSysDict> thSysDictList = this.service.queryList(wrapper);
            return ResponseModelFactory.OKWithData(thSysDictList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseModelFactory.error(e.getMessage());
        }

    }

    @PostMapping("/getTypeChildren")
    public ResponseModel getTypeChildren(@RequestBody ThSysDict dict) {
        try {
            QueryWrapper<ThSysDict> wrapper = new QueryWrapper<>();
            wrapper.eq("pid", dict.getPid());
            IPage<ThSysDict> iPage = this.service.queryList(new Page<>(dict.getPage(), dict.getRows()), wrapper);
            return ResponseModelFactory.OKWithData(new DataGrid(iPage.getTotal(), iPage.getRecords()));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseModelFactory.error(e.getMessage());
        }

    }

}
